//
//  JZHMyNameUpViewController.m
//  极致荟
//
//  Created by hetao on 14-8-19.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHMyNameUpViewController.h"
#import "OtherUsersDao.h"
@interface JZHMyNameUpViewController ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UINavigationItem *tileitm;
@property (weak, nonatomic) IBOutlet UIButton *deletebut;

@end

@implementation JZHMyNameUpViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (IBAction)savenaem:(id)sender
{
    [self updata:self.name.text];
}
- (IBAction)back:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (id)initUserType:(id)ouser
{
    self=[[JZHMyNameUpViewController alloc] init];
    self.ouser=ouser;
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    if(![self.ouser isKindOfClass:[OtherUsers class]])
    {
        self.name.text=[User defuceUser].name;
        self.tileitm.title=@"名字";
    }else
    {
        self.name.text=[(OtherUsers *)self.ouser bname];
        self.tileitm.title=@"设置备注";
    }
    
    [self.name becomeFirstResponder];
    [self.name setReturnKeyType:UIReturnKeyDone];
    
}
- (IBAction)deletetxt:(UIButton *)sender
{
    self.name.text=@"";
    self.deletebut.hidden=YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(range.location==0)
    {
        self.deletebut.hidden=YES;
    }else
    {
        self.deletebut.hidden=NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self updata:textField.text];
    return YES;
}

- (void)updata:(NSString *)text
{
    if(text.length<1)
    {
        return;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    [self.navigationController popViewControllerAnimated:YES];
    
    if([self.ouser isKindOfClass:[OtherUsers class]])
    {
        OtherUsers *ouserd=self.ouser;
        ouserd.bname=text;
        [OtherUsersDao insertData:ouserd];
        NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId",[NSNumber numberWithInt:[(OtherUsers *)self.ouser userId]],@"friendsId",text,@"remarkName" ,nil];
        [HttpRequest httpRequestPost:JZHHTTPURL(@"asterisk/addRemark") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
            if(rerurncode==0)
            {
                NSLog(@"%@",data);
            }
        }];
    }else
    {
        NSDictionary *dic=[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId",@"name",@"infoType",text,@"editInfo",nil];
        [User defuceUser].name=text;
        [HttpRequest httpRequestPost:JZHHTTPURL(@"myInformation/editInfo") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
            NSLog(@"%@",data);
        }];
    }
   

}
@end
